import RemoteProxy from "../net/proxy/RemoteProxy";

export default class CollisionHelper {




    /**
     * 必须在同一层级中
     * @param other 
     * @param self 
     * @param func 
     */
    static collision(self: cc.Node, other: cc.Node): boolean {
        let rect1 = self.getBoundingBox()
        let rect2 = other.getBoundingBox();
        let flag = this.isRectCollideRect(rect1, rect2);
        return flag;
    }

    static isRectInRect(rect: cc.Rect, area: cc.Rect) {
        return rect.x >= area.x &&
            rect.x + rect.width <= area.x + area.width &&
            rect.y >= area.y &&
            rect.y + rect.height <= area.y + area.height;
    }

    static isRectCollideRect(r1: cc.Rect, r2: cc.Rect) {
        let flag = !(r2.x + r2.width < r1.x || r2.y > r1.y + r1.height || r2.x > r1.x + r1.width || r2.y + r2.height < r1.y);
        if (flag) {
        }
        return flag;
    }

    static isRectCollide(r1, r2) {

        let flag = !(r2.right < r1.left || r2.bottom > r1.top || r2.left > r1.right || r2.top < r1.bottom);

        return flag;
    }

    static isPointInRect2(point: cc.Node, rect: cc.Rect) {

        return point.x <= rect.x + rect.width && point.x >= rect.x && point.y <= rect.y + rect.height && point.y >= rect.y;
    }

    static isPointInRect(point: cc.Node, rx: number, ry: number, rw: number, rh: number): boolean {
        return point.x <= rx + rw && point.x >= rx && point.y <= ry + rh && point.y >= ry;
    }

    static isPositionInRect(point: cc.Vec2, rect: cc.Rect) {
        cc.log(' point.x ', point.x, ' rect.x ', rect.x)
        let flag1 = point.x <= rect.x + rect.width
        let flag2 = point.x >= rect.x;
        let flag3 = point.y <= rect.y + rect.height;
        let flag4 = point.y >= rect.y;
        cc.log(flag1, flag2, flag3, flag4)
        return flag1 && flag2 && flag3 && flag4;
    }

    /**
     * 判断点是否在旋转后的矩形中
     * @param point 触摸点的坐标
     * @param node 碰撞节点，锚点必须为(0.5,0.5)
     */
    static isPosInRotationRect(point: cc.Vec2, node: cc.Node) {
        let hw = node.width / 2;
        let hh = node.height / 2
        let O = node.angle;
        let center = node.position;
        let X = point.x
        let Y = point.y
        let r = -O * (Math.PI / 180)
        let nTempX = center.x + (X - center.x) * Math.cos(r) - (Y - center.y) * Math.sin(r);
        let nTempY = center.y + (X - center.x) * Math.sin(r) + (Y - center.y) * Math.cos(r);
        if (nTempX > center.x - hw && nTempX < center.x + hw && nTempY > center.y - hh && nTempY < center.y + hh) {
            return true;
        }
        return false
    }




    static isXYInRect(x, y, rect) {
        return x <= rect.right && x >= rect.left && y <= rect.top && y >= rect.bottom;
    }


    /**
     * 物体是否已经出屏
     * @param x 
     * @param y 
     * @param size 
     */
    static isOutScreen(x: number, y: number, w: number, h: number, dw: number, dh: number): boolean {
        // let screen = cc.view.getFrameSize();
        let hw = w / 2;
        let hh = h / 2;

        let flag = x < -hw || x > dw + hw || y > dh + hh || y < - hh;
        if (flag) {
            // cc.log('isOutScreen width ',dw,' height ',dh,' x ',x,' y ',y,' flag ',flag)
        }
        return flag
    }


    /**
     * 物体是否已经出屏
     * @param x 
     * @param y 
     * @param size 
     */
    static isOutScreenExceptTop(x: number, y: number, w: number, h: number, dw: number, dh: number): boolean {
        // let screen = cc.view.getFrameSize();
        let hw = w / 2;
        let hh = h / 2;

        let flag = x < - hw || x > dw + hw || y < - hh;
        if (flag) {
            // cc.log('isEnemyOutScreen width ',dw,' height ',dh,' x ',x,' y ',y,' flag ',flag)
        }
        //
        return flag
    }
}
